<?php
/**[用户组表逻辑层]
 * @Author: chenli
 * @Email:  976123967@qq.com
 * @Date:   2015-02-23 15:41:02
 * @Last Modified by:   chenli
 * @Last Modified time: 2015-02-28 23:07:08
 */
class AuthGroupLogic extends Model{

	protected $tableName ='auth_group';




	// 自动验证
	protected $_validate = array(
		array('title','require','请输入用户组名称',1),
		array('title' ,'check_title','用户组已经存在',1,'callback'),
	);
	/**
	 * [check_title 验证用户组重复]
	 * @param  [type] $con [description]
	 * @return [type]      [description]
	 */
	protected function check_title($con)
	{
		$id  = I('post.id');
		if($id)
			$where['id'] = array('neq',$id);
		$where['title'] = $con;
		$data = $this->where($where)->find();
		if($data)
			return false;
		return true;
	}

	/**
	 * [add_auth_group 添加用户组]
	 */
	public function add_auth_group()
	{

		if(!$this->create())
			return false;
	
		return $this->add();
	}
	/**
	 * [edit_auth_group 编辑用户组]
	 */
	public function edit_auth_group()
	{

		if(!$this->create())
			return false;
		$this->save();
		return ture;
	}
	/**
	 * [find_all 查找所有用户组]
	 * @return [type] [description]
	 */
	public function find_all()
	{
		$data = $this->order('id asc')->select();
		if(!$data)
			return false;
		foreach($data as $k=>$v)
		{
			$data[$k]['status'] = $v['status']? '启用':'禁止';
		}
		return $data;

	}


	/**
	 * [find_one 查找一条记录]
	 * @return [type] [description]
	 */
	public function find_one()
	{
		$id = I('get.id');
		if(!$id)
		{
			$this->error= '链接错误';
			return fasle;
		}
		$where['id'] = $id;
		$data = $this->where($where)->find();
		if(!$data)
		{
			$this->error = '用户组不存在';
			return fasle;
		}

		return $data;

	}

	/**
	 * [del_auth_group 删除用户组]
	 * @return [type] [description]
	 */
	public function del_auth_group()
	{
		$id = I('get.id');
		if(!$id)
		{
			$this->error= '链接错误';
			return fasle;
		}
		$where['id'] = $id;
		$data = $this->where($where)->find();
		if(!$data)
		{
			$this->error = '用户组不存在';
			return fasle;
		}
		$this->delete($id);
		return ture;
	}

	/**
	 * [alter_rule 更新规则]
	 * @return [type] [description]
	 */
	public function alter_rule()
	{
		$rules = I('post.rules');
		$data['rules'] = implode(',', $rules);
		$data['id'] = I('post.id');

		$this->save($data);
		return true;
	}


}